﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.Model.Entity.UserCenter;
using Kovan.Model.Parameter.UserCenter;
using System.Linq.Expressions;
using Kovan.Library.Extensions;

namespace Kovan.DAL.UserCenter
{
    public class DActApplication : IDAL<act_application>
    {

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<act_application> GetList(PApplication param)
        {
            using (var db = new MysqlContext())
            {
                Expression<Func<act_application, bool>> wh = c => c != null;

                if (!string.IsNullOrEmpty(param.Name))
                {
                    wh = wh.And(c => c.name.Contains(param.Name));
                }

                var query = db.act_application.OrderByDescending(c => c.application_id.Value).AsQueryable();

                //总条数
                param.TotalRows = query.Where(wh).Count();

                if (param.PageSize > 0 && param.PageIndex > 0)
                {
                    query = query.Skip(param.PageSize * (param.PageIndex - 1)).Take(param.PageSize);
                }

                query = query.Where(wh);

                return param.TotalRows > 0 ? query.ToList() : null;
            }
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public  act_application GetModel(long id)
        {
            using (var db = new MysqlContext())
            {
                return db.act_application.FirstOrDefault(c => c.application_id == id);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public  int Add(act_application model)
        {
            using (var db = new MysqlContext())
            {
                db.act_application.Add(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public  int Delete(long id)
        {
            using (var db = new MysqlContext())
            {
                var model = db.act_application.FirstOrDefault(a => a.application_id == id);
                db.act_application.Remove(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public  int BatchDelete(IEnumerable<string> ids)
        {
            using (var db = new MysqlContext())
            {
                foreach (var id in ids)
                {
                    var applicationid = long.Parse(id);
                    var model = db.act_application.FirstOrDefault(a => a.application_id == applicationid);
                    if (model != null)
                    {
                        db.act_application.Remove(model);
                    }
                }
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public  int Update(act_application model)
        {
            using (var db = new MysqlContext())
            {
                db.Set<act_application>().Attach(model);
                db.Entry(model).State = System.Data.EntityState.Modified;
                return db.SaveChanges();
            }
        }
    }
}
